import { ScrollView, View, Text, Image, Input, Textarea, Video } from '@tarojs/components';
import React, { Component } from 'react';
import Taro, { getCurrentInstance } from '@tarojs/taro';
import { connect } from 'react-redux';
import './index.less';
import * as T from './types';
import actions from './actions';
import { store2Props } from './selectors';
import eyeIcon from '@/assets/image/index/eye.png';
import api from 'api';

actions().actions.loadReducer();
// @ts-ignore
@connect<Partial<T.IProps>, any>(store2Props, actions)
export default class Index extends Component<Partial<T.IProps>, any> {
  textInput: any;
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    this.props.actions.init();
  }

  componentWillUnmount() { }
  componentDidShow(): void {
    const title = Taro.getStorageSync("taskModel");
    if (title) {
      Taro.showToast({
        title,
        icon: 'none',
      });
      Taro.removeStorageSync("taskModel");
    }
  }
  async onShareAppMessage() {
    //触发分享，任务完成
    let task = Taro.getStorageSync('task');
    if (task) {
      task = JSON.parse(task)
      let id = task.id;
      const result = await api.TaskCenterController.completedTask({ id })
      // setTimeout(()=>{
      // Taro.showToast({
      //               title: `获得${task.taskInfoVO.rewardType == 0 ? task.taskInfoVO.rewardPoints : ""}${["积分", "优惠券", "实物商品"][task.taskInfoVO.rewardType]}${task.taskInfoVO.rewardType == 2 ? "请到我的-我的奖励中填写收货地址":""}`,
      //               icon: 'none',
      //             });
      Taro.setStorageSync("taskModel", `获得${task.taskInfoVO.rewardType == 0 ? task.taskInfoVO.rewardPoints : ""}${["积分", "优惠券", "实物商品"][task.taskInfoVO.rewardType]}${task.taskInfoVO.rewardType == 2 ? "请到我的-我的奖励中填写收货地址" : ""}`)
      Taro.removeStorageSync("task");
      // },5000)
    }
    let wechatShareInfo = JSON.parse(Taro.getStorageSync('wechatShareInfo'));
    return {
      title: wechatShareInfo.title,
      imageUrl: wechatShareInfo.imgUrl[0].url,
    };
  }

  render() {
    // 修改图片行内默认样式
    (Taro as any).options.html.transformElement = (el) => {
      if (el.nodeName === 'image') {
        el.setAttribute('mode', 'widthFix');
      }
      return el;
    };
    let {
      main: { detail },
      actions: { action },
    } = this.props;


    return (
      <ScrollView className="scrollView" scrollY>
        <View className="footerContentBox">
          <View className="footerContentTop">
            <View className="topBox">
              <View></View>
              <View className="visits">
                <Image src={eyeIcon} className="eyeIcon" mode="widthFix" />
                <View className="number">{detail.numberOfViewers}</View>
              </View>
            </View>
            <View className="activityTitle">{detail.activityTitle}</View>
          </View>
          <Image src={detail.activityImageUrl} className="footerContentBg" mode="widthFix" />
          <View className="subtitle">{detail.subtitle}</View>
        </View>
        <View className="activityContent" dangerouslySetInnerHTML={{ __html: detail.activityContent?.replace(/text-wrap: nowrap;/g, '') }} />
        {!!detail?.jumpAddress && (
          <Image
            onClick={() => {
              let { jumpAddress } = detail
              if (jumpAddress.includes("/pages")) {
                if (jumpAddress.includes("package-")) {
                  Taro.navigateTo({
                    url: jumpAddress,
                  });
                } else {
                  Taro.switchTab({
                    url: jumpAddress,
                  });
                }
              } else {
                Taro.showToast({
                  title: '链接不存在',
                  icon: 'none'
                })
              }
            }}
            src={'https://ruiyinmall.oss-cn-nanjing.aliyuncs.com/round_link_fill.png'}
            className="round"
            mode="widthFix"
          />
        )}

      </ScrollView>
    );
  }
}
